<template>
  <div class="account-data">
    <div class="add-box">
      <el-button @click="addAccount" type="primary">新增账户</el-button>
    </div>
    <!-- 数据列表 -->
    <el-table :data="tableData" border style="width:100%" v-loading="loading" element-loading-text="拼命加载中">
      <el-table-column label="角色" width="180">
        <template slot-scope="scope">
          <span>{{ roleName(scope.row.roleId).name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="账号" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.userAccount}}</span>
        </template>
      </el-table-column>

      <el-table-column label="姓名">
        <template slot-scope="scope">
          <span>{{scope.row.nickName}}</span>
        </template>
      </el-table-column>

      <el-table-column label="描述">
        <template slot-scope="scope">
          <span>{{ roleName(scope.row.roleId).describe }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="180">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1 && getUser.roleId ===1">
          <el-button @click="handleDelete(scope.row)" size="mini" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pages" ref="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        layout="prev, pager, next"
        :page-size="pages"
        :total="total">
      </el-pagination>
    </div>

    <!-- 新增账户 -->
    <el-dialog
      title="新增账户"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <el-form :rules="rules" ref="ruleForm" :model="account" class="form-box" >
        <el-form-item label="请选择角色" prop="role">
          <el-select @change="change" v-model="account.role" placeholder="请选择角色">
            <el-option
              v-for="option in options"
              :label="option.role"
              :value="option.key"
              :key="option.key"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="请输入账号" prop="userAccount">
          <el-input v-model="account.userAccount" placeholder="请输入账号"></el-input>
        </el-form-item>

        <el-form-item label="请输入昵称" prop="nickName">
          <el-input v-model="account.nickName" placeholder="请输入昵称"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button @click="handleAdd" type="primary" :loading="loading">确定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import jwt_decode from 'jwt-decode';

@Component({
  components: {  }
})
export default class AccountData extends Vue {
  getUser: any;
  tableData: any = [];
  total:number = 0; // 总数据量
  pages:number = 3; // 每页数据量
  pageNumber: number = 0; // 页码
  loading:boolean = true;// loading加载
  dialogVisible: Boolean = false; // 新增账户模态框显示状态


  // 账户添加
  account: any = {
    userAccount:'',
    nickName:'',
    roleId:'',
    role:'',
    password:'123456'
  };

  rules: any = {
    role: [{ required: true, message: "请选择角色", trigger: "change" }],
    userAccount: [{ required: true, message: "请输入账号", trigger: "blur" }],
    nickName: [{ required: true, message: "请输入昵称", trigger: "blur" }],
  };

  // select数据
  options: any = [];

  created() {
    this.getUser = jwt_decode(localStorage.tsToken);
    this.roleUser();
    this.getData();
  }

  // 根据角色判断添加账户角色
  roleUser(){
    this.getUser = jwt_decode(localStorage.tsToken);
    if(this.getUser.roleId === 1){
      this.options = [
        {
          key: 1,
          role: "系统管理员",
        },
        {
          key: 2,
          role: "管理员",
        },
        {
          key: 3,
          role: "普通用户",
        }
      ]
    }
    else{
      this.options = [
        {
          key: 2,
          role: "管理员",
        },
        {
          key: 3,
          role: "普通用户",
        }
      ]
    }
  }

  // 新增账户
  handleAdd() {
    (this as any).$refs["ruleForm"].validate((valid: boolean) => {
      if (valid) {
        this.loading = true;
        const { userAccount, nickName, roleId, password } = this.account;
        const param:any = { userAccount, nickName, roleId, password };

        (this as any).$axios.post("/adminUser/add", param).then((res: any) => {
          if(res.data.code === 200){
            this.getData();
            this.closeDialog();
            this.loading = false;
            (this as any).$message({
              message: '账户添加成功',
              type: "success"
            })
          }
          else{
            (this as any).$message.error(res.data.data)
          }
        })
      }
    });
  }

  // 下拉框时间 - 选择角色
  change(e:any){
    this.account.roleId = e;
  }

  // 显示新增账户模态框
  addAccount() {
    this.dialogVisible = true;
  }

  // 隐藏新增账户
  closeDialog() {
    this.dialogVisible = false;
  }

  // 删除
  handleDelete(row: any): void {
    (this as any).$confirm('是否确认删除？')
    .then((res:any)=> {
      const { id } = row;

      (this as any).$axios.post('/adminUser/userDel', { id }).then((res: any) => {
        if(res.data.code === 200){
            this.getData();
            (this as any).$message({
              message: res.data.data,
              type: "success"
            })
          }
          else{
            (this as any).$message.error(res.data.data)
          }
      })
    })
    
  }

  // 数据显示
  getData() {
    this.loading = true;
    const param = {
      pages: this.pages,
      pageNumber: this.pageNumber
    };

    (this as any).$axios("/adminUser/userList", { params:param }).then((res: any) => {
      this.loading = false;

      if(res.data.code === 200){
        this.tableData = res.data.data.list;
        this.total = res.data.data.sum;
      }
    })
  }

  // 分页 pageSize 改变时会触发
  handleSizeChange(val: number) {
    if(val === 1){
      this.pageNumber = 0
    }
    else{
      this.pageNumber = (val-1) * this.pages;
    }
    this.getData();
  }
  // 分页 currentPage 改变时会触发
  handleCurrentChange(val: number) {
    if(val === 1){
      this.pageNumber = 0
    }
    else{
      this.pageNumber = (val-1) * this.pages;
    }
    this.getData();
  }

  // 角色显示名称
  roleName(roleId:number){
    let obj = {
      name:'',
      describe:''
    };

    switch(roleId){
      case 1: obj = { name:'系统管理员', describe:'拥有系统的所有权限' }; break;
      case 2: obj = { name:'管理员', describe:'账户信息权限，无删除权限' }; break;
      case 3: obj = { name:'普通用户', describe:'普通用户权限' }; break;
    }
    return obj;
  }
}
</script>

<style lang="scss" scoped>
.account-data {
  height: 100%;
  overflow: auto;
  .add-box {
    margin-bottom: 10px;
  }
}
.pages{
  width: 100%;
  margin-top: 10px;
  background: white;
  display: flex;
  justify-content: flex-end;
  padding: 20px 0;
}

.form-box {
  .el-input,
  .el-select {
    width: 400px !important;
  }
}
</style>
